<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 用户列表主体 -->
        <!-- 卡片视图区 -->
        <el-card>
            <el-row :gutter="20">
                <!-- 输入框 -->
                <el-col :span="10">
                    <el-input placeholder="请输入内容" clearable @clear="getAllUser" v-model="queryInfo.query">
                        <el-button slot="append" icon="el-icon-search" @click="getNewUserList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="addDiglogVisible=true">添加用户</el-button>
                </el-col>
            </el-row>

            <!-- 用户列表 border 边框 stripe 隔行变色 -->
            <el-table :border="true" stripe :data="userlist">
                <el-table-column type="index"></el-table-column>
                <!--索引列-->
                <el-table-column label="姓名" prop="username"></el-table-column>
                <el-table-column label="邮箱" prop="email"></el-table-column>
                <el-table-column label="密码" prop="password"></el-table-column>
                <el-table-column label="角色" prop="role"></el-table-column>
                <el-table-column label="状态" prop="state">
                    <!-- 作用域插槽 -->
                    <template slot-scope="scope">
                        <!-- scope.row每一行封装的数据  userStateChanged更改用户状态，并把当前行传参-->
                        <el-switch v-model="scope.row.state" @change="userStateChanged(scope.row)"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <!-- 修改 -->
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="showDialog(scope.row)"></el-button>
                        <!-- 删除 -->
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="delUser(scope.row.id)">
                        </el-button>
                        <!-- 权限 -->
                        <el-tooltip effect="dark" content="分配权限" placement="top-start" :enterable="false">
                            <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页组件 page-size每页显示条目个数 	每页显示个数选择器的选项设置-->
            <!-- size-change pageSize 改变时会触发 每页条数 -->
            <!-- current-change currentPage 改变时会触发 当前页 -->
            <div>
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :page-sizes="[1, 2, 5, 100]" :page-size="queryInfo.pageSize" :current-page="queryInfo.pageNum"
                    layout="total, sizes, prev, pager, next, jumper" :total="total">
                </el-pagination>
            </div>
        </el-card>
        <!-- 新增用户弹出表单 -->
        <el-dialog title="添加用户" :visible.sync="addDiglogVisible" @close="addDialogClose" width="50%">
            <el-form :model="addForm" :rules="addRules" ref="addFormRef" label-width="70px">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="addForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="addForm.password"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="addForm.email"></el-input>
                </el-form-item>
            </el-form>
            <!-- 弹出表单底部的按钮 -->
            <div slot="footer" class="dialog-footer">
                <el-button @click="addDiglogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addUser">确 定</el-button>
            </div>
        </el-dialog>

        <!-- 修改用户信息弹出表单 -->
        <el-dialog title="修改用户信息" :visible.sync="editDiglogVisible" @close="editDialogClose" width="50%">
            <el-form :model="editForm" :rules="editRules" ref="editFormRef" label-width="70px">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="editForm.username" disabled></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="editForm.password"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="editForm.email"></el-input>
                </el-form-item>
            </el-form>
            <!-- 弹出表单底部的按钮 -->
            <div slot="footer" class="dialog-footer">
                <el-button @click="editDiglogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editUser">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() { 
        return {
            // 请求数据
            queryInfo: {
                query: "",//当前查询内容
                pageNum: 1,//当前页
                pageSize: 5//页面记录数
            },
            userlist: [],// 用户列表
            total: 0, // 最大数据记录
            addDiglogVisible: false,//弹出添加用户表单是否可见
            editDiglogVisible: false,//弹出修改用户表单是否可见
            //保存添加用户表单的信息
            addForm: {
                username: "",
                password: "",
                email: ""
            },
            //保存修改用户表单的信息
            editForm: {
            },
            // 添加用户表单校验
            addRules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 7, message: '长度在 3 到 7 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { min: 5, max: 17, message: '长度在 5 到 17 个字符', trigger: 'blur' }
                ]
            },
            // 修改用户表单校验
            editRules: {
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { min: 5, max: 17, message: '长度在 5 到 17 个字符', trigger: 'blur' }
                ]
            }
          
        }
    },
    created() {
        this.getAllUser();
    },
    methods: {
        getAllUser() { 
                this.axios.get('/alluser', { params: this.queryInfo}).then((res) => {
                this.userlist = res.data.data; // 将返回数据赋值
                this.total = res.data.numbers; // 总个数
            })
        },
        //当前页数最大记录数发生改变
        handleSizeChange(newSize) { 
            this.queryInfo.pageSize = newSize
            this.getAllUser()
        },
        handleCurrentChange(newPage) { 
            this.queryInfo.pageNum = newPage
            this.getAllUser()
        },
        getNewUserList() { 
            this.queryInfo.pageNum = 1
            this.getAllUser()
        },

        //修改用户状态
        userStateChanged(userinfo) {
            this.axios.put(`/userstate/${userinfo.id}/${userinfo.state}`).then((res) => { 
                if (res.data != 'success') { 
                    this.userinfo = !this.userinfo;
                   return  this.$message.error("操作失败!")
                }
                this.$message.success("操作成功!")
            })
        },

        // 添加用户
        addUser() { 
            this.$refs.addFormRef.validate((valid) => { 
                if (!valid) return
                this.axios.post('/addUser', this.addForm).then((res) => {
                    const data = res.data;
                    if (data != 'success') return this.$message.error("操作失败");
                    this.$message.success("操作成功");
                    this.addDiglogVisible = false;
                    this.getAllUser();
                })
                
            })
        },
        addDialogClose(){ 
            this.$refs.addFormRef.resetFields();
        },
        //删除用户
        delUser(id) { 
            const confirmResult=this.$confirm("此操作将永久删除用户信息,是否继续", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type:'warning'               

            }).then(() => { 
                this.axios.delete(`/deluser/${id}`).then((res) => {
                    const data = res.data
                    if (data != "success") return this.$message.error("数据删除失败!");
                    this.$message.success("删除成功");
                }).finally(() => {
                    this.getAllUser();
                })
            }).catch(() => {
                if (confirmResult != 'confirm') { 
                    return this.$message.info("取消操作");
                }
             
            })
        },
        //显示对话框
        showDialog(row) { 
            this.editForm = row//修改表单的数据就是当前行的数据
            this.editDiglogVisible=true//可见
        },
        //修改用户
        editUser() { 
            this.$refs.editFormRef.validate((valid) => { 
                if (!valid) return
                //发起请求
                this.axios.put("/editUser", this.editForm).then((res) => { 
                    const data = res.data
                    if (data != 'success') { 
                        return this.$message.error("修改失败!")
                    }
                    this.$message.success("修改成功!")
                    //隐藏
                    this.editDiglogVisible = false;
                    //刷新
                    this.getAllUser()
                })
            })
        },
        //取消时的操作
        editDialogClose() { 
            this.$refs.editFormRef.resetFields()
        }
    }

}
</script>

<style lang="less" scoped>




        /* 面包屑样式 */
        .el-breadcrumb {
            margin-bottom: 15px;
            font-size: 17px;
        }
        //搜索框和按钮居中显示
        .el-row{
            display: flex;
            justify-content: center;
        }
        .el-card__body .el-row{
            margin:20px;
        }
</style>